//
//  KTCheckbox.m
//  BoWuGuan
//
//  Created by 梁尚嘉 on 2016/11/4.
//  Copyright © 2016年 WUTOBE. All rights reserved.
//

#import "KTCheckbox.h"
@interface KTCheckbox ()
@property (nonatomic, strong) UILabel *element;
@property (nonatomic, strong) UIButton *button;
@end


@implementation KTCheckbox

- (instancetype)init
{
    if (self = [super init]) {
        [self configure];
    }
    return self;
}

- (void)configure
{
    [self addSubview:self.element];
    [self addSubview:self.button];

    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(SM_SIZE(35), SM_SIZE(35)));
    }];

    [self.element mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.button.mas_right).offset(SM_SIZE(5));
        make.top.right.bottom.equalTo(@0);
    }];
}


- (UILabel *)element
{
    if (!_element) {
        _element                        = [UILabel new];
        _element.text                   = @"未定义";
        _element.font                   = SM_CFont(30);
        _element.textColor              = SM_blackColor;
        _element.highlightedTextColor   = SM_blackColor;
    }
    return _element;
}

- (UIButton *)button
{
    if (!_button) {
        _button = [UIButton new];
        [_button setImage:[[UIImage imageNamed:@"btn_shoppingcart"] imageWithColor:SM_lightGrayColor]
                 forState:UIControlStateNormal];
        [_button setImage:[UIImage imageNamed:@"btn_shoppingcart_hl"]
                 forState:UIControlStateSelected];
        _button.userInteractionEnabled = NO;
    }
    return _button;
}

- (void)setSelected:(BOOL)selected
{
    _selected               = selected;
    _button.selected        = selected;
    _element.highlighted    = selected;
}

- (void)setTitle:(NSString *)title
{
    _title              = title;
    self.element.text   = title;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject]locationInView:self.superview];
    if (CGRectContainsPoint(self.frame, point)) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(checkBox:didClick:)]) {
            [self.delegate checkBox:self didClick:false];
        }
    }
}
@end
